package com.gqz.equip.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.common.core.utils.query.QueryMapperUtil;
import com.gqz.equip.app.entity.TEnWzEquipEntity;
import com.gqz.equip.entity.EnStockEquipDefectEntity;
import com.gqz.equip.mapper.EnStockEquipDefectMapper;
import com.gqz.equip.mapstruct.EnStockEquipDefectMapstruct;
import com.gqz.equip.service.IEnStockEquipDefectService;
import com.gqz.equip.vo.EnStockEquipDefectVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 能源-盘点任务设备缺陷表 ҵ���߼�ʵ�ֲ�ServiceImpl
 *
 * @Version 1.0
 * @Author input you name
 */
@Service
@Slf4j
public class EnStockEquipDefectServiceImpl extends ServiceImpl<EnStockEquipDefectMapper, EnStockEquipDefectEntity> implements IEnStockEquipDefectService {

    @Resource
    private EnStockEquipDefectMapstruct mapstruct;

    @Override
    public List<EnStockEquipDefectVo> getById(String id) {
        MPJLambdaWrapper<EnStockEquipDefectEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(EnStockEquipDefectEntity::getId, id);
        queryWrapper.selectAll(EnStockEquipDefectEntity.class);
        List<EnStockEquipDefectEntity> entities = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(entities)) {
            return mapstruct.toVos(entities);
        }
        return new ArrayList<>();
    }

    @Override
    public Page<EnStockEquipDefectVo> page(RequestForm<EnStockEquipDefectVo> requestForm) {
        EnStockEquipDefectEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnStockEquipDefectEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.selectAll(EnStockEquipDefectEntity.class);
        queryWrapper.selectAs(EnStockEquipDefectEntity::getUnitName, "defectUnitName");
        queryWrapper.selectAs(EnStockEquipDefectEntity::getUnitNo, "defectUnitNo");
        queryWrapper.selectAs(TEnWzEquipEntity::getGldwmc, "equipUnitName");
        queryWrapper.selectAs(TEnWzEquipEntity::getGldwmc, "equipUnitNo");
        queryWrapper.select(TEnWzEquipEntity::getCcbm);
        queryWrapper.innerJoin(TEnWzEquipEntity.class, TEnWzEquipEntity::getSbbm, EnStockEquipDefectEntity::getEquipNo);
        Page<EnStockEquipDefectEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        IPage<EnStockEquipDefectEntity> iPage = baseMapper.selectJoinPage(page, EnStockEquipDefectEntity.class, queryWrapper);
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return mapstruct.toVos(page);
    }

    @Override
    public Long count(RequestForm<EnStockEquipDefectVo> requestForm) {
        EnStockEquipDefectEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnStockEquipDefectEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.select(EnStockEquipDefectEntity::getId);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(List<EnStockEquipDefectVo> forms) {
        List<EnStockEquipDefectEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnStockEquipDefectEntity entity = entitys.get(i);
            result += baseMapper.insert(entity);
        }
        return result;
    }

    @Override
    public int update(List<EnStockEquipDefectVo> forms) {
        List<EnStockEquipDefectEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnStockEquipDefectEntity entity = entitys.get(i);
            result += baseMapper.updateById(entity);
        }
        return result;
    }


}
